package cn.yunhe.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.yunhe.dao.UserDao;
import cn.yunhe.entity.User;
import cn.yunhe.util.C3P0Util;

/**
 * 用户相关 数据访问类
 * 
 * @author Administrator
 *
 */
public class UserDaoImpl implements UserDao {

	@Override
	public User login(String uname, String pwd) {
		User user = null;
		Connection conn = C3P0Util.getConnection();
		StringBuilder sql = new StringBuilder();
		sql.append("select uid,uname,pwd,state,flag ");
		sql.append(" from tb_user");
		sql.append(" where uname = ? and pwd = ?");
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, uname);
			ps.setString(2, pwd);
			rs = ps.executeQuery();
			if (rs.next()) {
				user = new User();
				user.setId(rs.getInt("uid"));
				user.setLoginName(rs.getString("uname"));
				user.setPwd(rs.getString("pwd"));
				user.setState(rs.getInt("state"));
				user.setFlag(rs.getInt("flag"));
				 System.out.println(user.getFlag()+"==========");
			}
			return user;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return user;
		} finally {
			C3P0Util.close(rs, ps, conn);
		}

	}

	@Override
	public boolean register(User user) {
		int num = -1;
		Connection conn = C3P0Util.getConnection();
		StringBuilder sql = new StringBuilder();
		sql.append(" insert into tb_user(uname,pwd,state,flag) ");
		sql.append(" values(?,?,?,?)");
		PreparedStatement ps = null;
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, user.getLoginName());
			ps.setString(2, user.getPwd());
			ps.setInt(3, user.getState());
			ps.setInt(4, user.getFlag());
			num = ps.executeUpdate();
			conn.commit();
			if (num > 0) {
               return true;
			}else{
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally {
			C3P0Util.close(null, ps, conn);
		}
		 
	}

	@Override
	public List<User> findAllUser() {
		List<User> list = new ArrayList<User>();
		Connection conn = C3P0Util.getConnection();
		StringBuilder sql = new StringBuilder();
		sql.append(" select uid,uname,pwd,state,flag ");
		sql.append(" from tb_user");
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			while (rs.next()) {
				User user = new User();
				user.setId(rs.getInt("uid"));
				user.setLoginName(rs.getString("uname"));
				user.setPwd(rs.getString("pwd"));
				user.setState(rs.getInt("state"));
				user.setFlag(rs.getInt("flag"));
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return list;
		} finally {
			C3P0Util.close(rs, ps, conn);
		}
	}

	@Override
	public boolean lockUnlockUser(int uid,boolean lock) {
		int num = -1;
		Connection conn = C3P0Util.getConnection();
		StringBuilder sql = new StringBuilder();
		sql.append(" update tb_user ");
		if(lock){
			sql.append(" set state = 1");
		}else{
			sql.append(" set state = 0");
		}
		sql.append(" where uid = ?");
		PreparedStatement ps = null;
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql.toString());	 
			ps.setInt(1, uid);
			num = ps.executeUpdate();
			conn.commit();
			if (num > 0) {
               return true;
			}else{
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally {
			C3P0Util.close(null, ps, conn);
		}
	}

}
